package com.cloud.server.service.order.impl;

import com.cloud.common.CloudUserUtils;
import com.cloud.common.OrderUtils;
import com.cloud.server.domain.order.IntegralType;
import com.cloud.server.domain.order.OrderIntegralDetails;
import com.cloud.server.domain.order.OrderType;
import com.cloud.server.domain.user.UserIntegral;
import com.cloud.server.from.order.OrderIntegralDetailsFrom;
import com.cloud.server.from.user.PraiseFrom;
import com.cloud.server.mapper.order.OrderIntegralDetailsMapper;
import com.cloud.server.service.order.OrderIntegralDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.server.service.user.UserIntegralService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyuan
 * @since 2021-11-28
 */
@Service
public class OrderIntegralDetailsServiceImpl extends ServiceImpl<OrderIntegralDetailsMapper, OrderIntegralDetails> implements OrderIntegralDetailsService {

    @Autowired
    OrderIntegralDetailsMapper orderIntegralDetailsMapper;
    @Autowired
    UserIntegralService userIntegralService;

    @Override
    @Transactional
    public boolean praise(PraiseFrom praiseFrom) throws Exception {
        Long userId = praiseFrom.getUserId();
        /**
         * 创建订单
         * */
        OrderIntegralDetails orderIntegralDetails = new OrderIntegralDetails();
        UserIntegral userIntegral = userIntegralService.getByUserId(userId);
        orderIntegralDetails.setOperationType(IntegralType.BONUS);
        if(praiseFrom.getType().equals(OrderType.CLASS)){
            orderIntegralDetails.setOrderType(OrderType.CLASS_PRAISE);
        }else if(praiseFrom.getType().equals(OrderType.DORMITORY)){
            orderIntegralDetails.setOrderType(OrderType.DORMITORY_PRAISE);
        }

        boolean createOrder = CreateOrder(orderIntegralDetails,praiseFrom,userIntegral.getId());
        if(createOrder){
            Long score = praiseFrom.getScore();
            Long integralTotal = userIntegral.getIntegralTotal();
            long nowIntegralTotal = integralTotal + score;
            UserIntegral userIntegral1 = userIntegral.setIntegralTotal(nowIntegralTotal);
            return userIntegralService.updateById(userIntegral1);

        }
        return false;
    }

    @Override
    @Transactional
    public boolean criticism(PraiseFrom praiseFrom) throws Exception {
        Long userId = praiseFrom.getUserId();
        UserIntegral userIntegral = userIntegralService.getByUserId(userId);
        OrderIntegralDetails orderIntegralDetails = new OrderIntegralDetails();
        orderIntegralDetails.setOperationType(IntegralType.DEDUCT);
        if(praiseFrom.getType().equals(OrderType.CLASS)){
            orderIntegralDetails.setOrderType(OrderType.CLASS_CRITICISM);
        }else if(praiseFrom.getType().equals(OrderType.DORMITORY)){
            orderIntegralDetails.setOrderType(OrderType.DORMITORY_CRITICISM);
        }
        boolean createOrder = CreateOrder(orderIntegralDetails,praiseFrom,userIntegral.getId());
        if(createOrder){
            Long score = praiseFrom.getScore();
            Long integralTotal = userIntegral.getIntegralTotal();
            long nowIntegralTotal = integralTotal - score;
            UserIntegral userIntegral1 = userIntegral.setIntegralTotal(nowIntegralTotal);
            return userIntegralService.updateById(userIntegral1);

        }
        return false;
    }

    @Override
    public List<OrderIntegralDetails> getByUserId(OrderIntegralDetailsFrom orderIntegralDetailsFrom) throws Exception {
        Long userId = CloudUserUtils.getUserId();
        Map<String, Object> queryHashMap = new HashMap<>();
        queryHashMap.put("user_id",userId);
        queryHashMap.put("order_type",orderIntegralDetailsFrom.getOrderType());
        return orderIntegralDetailsMapper.selectByMap(queryHashMap);
    }


    public boolean CreateOrder( OrderIntegralDetails orderIntegralDetails,PraiseFrom praiseFro,Long id) throws Exception {
        Long userId = CloudUserUtils.getUserId();
        BeanUtils.copyProperties(praiseFro,orderIntegralDetails);
        orderIntegralDetails.setOrderSeq(OrderUtils.getOrderCode(praiseFro.getUserId()));
        orderIntegralDetails.setContent(praiseFro.getRemark());
        orderIntegralDetails.setOperationId(userId);
        orderIntegralDetails.setIntegralId(id);
        int insert = orderIntegralDetailsMapper.insert(orderIntegralDetails);
        if(insert != 1){
            return false;
        }
        return true;
    }
}
